#include "MailController.h"
#include"event/SignalManager.h"
#include"MessageIDConst.h"
#include"dbconnect/Model.h"
#include"dbconnect/DBDao.h"
#include"ServerMaster.h"
#include"DBMaster.h"
#include"Common.h"
#include"MessageErrorConst.h"
#include"GameServer.h"
#include"utils/GameUtils.h"
#include"UserBaseController.h"
#include"DropManager.h"

void mailRetContent(std::vector<DBModel::Mails> list,Json::Value& out)
{
	Json::Value mails;
	int size = list.size();
	for (int i = 0; i < size; ++i)
	{
		auto item = list[i];
		Json::Value mail;
		mail["id"] = item.id;
		mail["title"] = item.title;
		mail["content"] = item.content;
		auto listType = GameUtils::split(item.item_type, "|");
		auto listValue = GameUtils::split(item.item_value, "|");
		auto listSize = GameUtils::split(item.item_size, "|");
		mail["awards"] = Json::Value();
		int nawards = listType.size();
		for (int j = 0; j < nawards; ++j)
		{
			Json::Value award;
			award["type"] = atoi(listType[j].c_str());
			award["value"] = atoi(listValue[j].c_str());
			award["size"] = atoi(listSize[j].c_str());
			mail["awards"].append(award);
		}
		mails.append(mail);
	}
	out["mails"] = mails;
}

void appendNullAward(Json::Value& out)
{
	out["awards"] = Json::Value();
	Json::Value award = Json::Value();
	award["type"] = 0;
	award["value"] = 0;
	award["size"] = 0;
	out["awards"].append(award);
}

MailController::MailController(Object* master)
{
	m_master = master;
	SignalManager::getInstance()->add(ID_C2S_ReceivetMail, handler(this, SEL_EVENTFUNC(MailController::c2sReceivetMail)));
	SignalManager::getInstance()->add(ID_C2S_GetAllMail, handler(this, SEL_EVENTFUNC(MailController::c2sGetAllMail)));
	SignalManager::getInstance()->add(ID_C2S_GetMail, handler(this, SEL_EVENTFUNC(MailController::c2sGetMail)));
}

MailController::~MailController()
{
}

int MailController::addMail(const std::string& title, const std::string& content,const std::string& item_type,const std::string& item_value,const std::string& item_size)
{
	auto master = dynamic_cast<ServerMaster*>(m_master);
	auto globalId = master->getDBMaster()->getBlobalId("mails");
	if (globalId == 0)
	{
		return 0;
	}
	DBModel::Mails mail;
	mail.id = globalId;
	mail.title = title;
	mail.content = content;
	mail.item_type = item_type;
	mail.item_value = item_value;
	mail.item_size = item_size;
	DBDao<DBModel::Mails> dao(master->getDBMaster()->getSqlConnnect());
	dao.setModel(mail);
	if (dao.insertModel())
	{
		return globalId;
	}
	else
	{
		return 0;
	}
}

void MailController::c2sReceivetMail(Json::Value& message)
{
	Json::Value mailRet;
	mailRet[MESSAGE_ID] = ID_S2C_ReceivetMail;
	auto master = dynamic_cast<ServerMaster*>(m_master);
	auto sock = message[RECV_SOCK].asInt64();

	int mailId = message["id"].asInt();
	DBModel::Mails mail;
	mail.id = mailId;
	DBDao<DBModel::Mails> dao(master->getDBMaster()->getSqlConnnect());
	dao.setModel(mail);
	auto list = dao.queryModel();
	if (list.size() == 1)
	{
		auto myMail = list[0];
		auto listType = GameUtils::split(myMail.item_type, "|");
		auto listValue = GameUtils::split(myMail.item_value, "|");
		auto listSize = GameUtils::split(myMail.item_size, "|");
		size_t size = listType.size();
		mailRet["ret"] = MessageRet_Success;
		mailRet["id"] = mailId;
		Json::Value awards;
		for (size_t i = 0; i < size; ++i)
		{
			Json::Value award;
			award["type"] = std::atoi(listType[0].c_str());
			award["value"] = std::atoi(listValue[0].c_str());
			award["size"] = std::atoi(listSize[0].c_str());
			awards.append(award);
		}
		mailRet["awards"] = awards;
		master->getGameServer()->Send(sock, mailRet);
	}
	else
	{
		mailRet["ret"] = MessageError_MailFail;
		mailRet["id"] = 0;
		appendNullAward(mailRet);
		master->getGameServer()->Send(sock, mailRet);
	}
}

void MailController::c2sGetMail(Json::Value& message)
{
	Json::Value mailRet;
	mailRet[MESSAGE_ID] = ID_S2C_GetMail;
	auto master = dynamic_cast<ServerMaster*>(m_master);
	auto sock = message[RECV_SOCK].asInt64();
	auto userBase = master->getModule<UserBaseController>();
	int userId = userBase->geUserIdBySock(sock);
	if (userId)
	{
		mailRet["ret"] = MessageError_UserNotFound;
		std::vector<DBModel::Mails> ret;
		DBModel::Mails mail;
		mail.item_type = "0";
		mail.item_value = "0";
		mail.item_size = "0";
		ret.push_back(mail);
		mailRetContent(ret, mailRet);
		master->getGameServer()->Send(sock, mailRet);
		return;
	}
	DBModel::Mails mail;
	mail.userId = userId;
	DBDao<DBModel::Mails> dao(master->getDBMaster()->getSqlConnnect());
	dao.setModel(mail);
	auto list = dao.queryModel();
	if (list.size() > 0)
	{
		mailRet["ret"] = MessageRet_Success;
		mailRetContent(list, mailRet);
	}
	else
	{
		mailRet["ret"] = MessageError_MailFail;
		std::vector<DBModel::Mails> ret;
		DBModel::Mails mail;
		mail.item_type = "0";
		mail.item_value = "0";
		mail.item_size = "0";
		ret.push_back(mail);
		mailRetContent(ret, mailRet);
	}
	master->getGameServer()->Send(sock, mailRet);
}

void MailController::c2sGetAllMail(Json::Value& message)
{
	Json::Value mailRet;
	auto master = dynamic_cast<ServerMaster*>(m_master);
	mailRet[MESSAGE_ID] = ID_S2C_GetAllMail;
	auto sock = message[RECV_SOCK].asInt64();
	auto userBase = master->getModule<UserBaseController>();
	int userId = userBase->geUserIdBySock(sock);
	if (userId == 0)
	{
		mailRet["ret"] = MessageError_UserNotFound;
		appendNullAward(mailRet);
		master->getGameServer()->Send(sock, mailRet);
		return;
	}
	DBModel::Mails mail;
	mail.userId = userId;
	DBDao<DBModel::Mails> dao(master->getDBMaster()->getSqlConnnect());
	dao.setModel(mail);
	auto list = dao.queryModel();
	if (list.size() > 0)
	{
		mailRet["ret"] = MessageRet_Success;
		mailRet["awards"] = Json::Value();
		size_t awardCount = list.size();
		for (int i = 0; i < awardCount; ++i)
		{
			auto item = list[0];
			auto listType = GameUtils::split(item.item_type, "|");
			auto listValue = GameUtils::split(item.item_value, "|");
			auto listSize = GameUtils::split(item.item_size, "|");
			size_t countAw = listType.size();
			for (int j = 0; j < countAw; ++j)
			{
				Json::Value award;
				award["type"] = atoi(listType[j].c_str());
				award["value"] = atoi(listValue[j].c_str());
				award["size"] = atoi(listSize[j].c_str());
				mailRet["awards"].append(award);
			}
		}
	}
	else
	{
		mailRet["ret"] = MessageError_MailFail;
		appendNullAward(mailRet);
	}
	master->getGameServer()->Send(sock, mailRet);
}
